package com.supercube.wechat.controller;

import com.supercube.common.utils.R;
import com.supercube.common.utils.StringUtils;
// import com.supercube.crm.vo.CourseVO;
// import com.supercube.crm.vo.GoodsVO;
import com.supercube.sport.yoga.dao.*;
import com.supercube.sport.yoga.domain.*;
import com.supercube.sport.yoga.service.VenueInfoService;
import com.supercube.sport.yoga.vo.MemberCardBasicInfoBo;
import com.supercube.sport.yoga.vo.MemberCardBasicInfoVO;
import com.supercube.sport.yoga.vo.MemberVO;
import com.supercube.sport.yoga.vo.VenueInfoVO;
import com.supercube.system.domain.UserDO;
import com.supercube.wechat.domain.CustomPageDO;
import com.supercube.wechat.domain.NotificationDO;
import com.supercube.wechat.domain.ShopsDO;
import com.supercube.wechat.domain.SlideImageDO;
import com.supercube.wechat.response.AppointmentClassVO;
import com.supercube.wechat.response.ClassInfoVO;
import com.supercube.wechat.response.RecommendCourseVO;
import com.supercube.wechat.service.MiniAppApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 小程序Api控制器<br />
 * 小程序的所有Api写在此处
 *
 * @author djzhao
 * @date 19/11/01 22:13
 * @email djzhao627@gmail.com
 */
@RestController
@RequestMapping("/miniapp")
public class MiniAppApiController {

    @Autowired
    private MiniAppApiService miniAppApiService;

    @Autowired
    private VenueInfoService venueInfoService;

    @Autowired
    private MemberIntegralRecordMapper memberIntegralRecordMapper;

    @Autowired
    private MemberBodyInfoMapper memberBodyInfoMapper;

    @Autowired
    private BookingCourseMapper bookingCourseMapper;

    // ===============首页相关===============

    /**
     * 获取首页轮播图
     *
     * @return 轮播图列表
     */
    @PostMapping("/slideImage")
    public R getHomeSlideImage() {
        List<SlideImageDO> homeSlideImage = miniAppApiService.getHomeSlideImage();
        if (homeSlideImage != null && homeSlideImage.size() > 0) {
            R ok = R.ok();
            ok.put("images", homeSlideImage);
            return ok;
        }
        return R.error();
    }

    /**
     * 获取通知
     *
     * @param num 需要获取的数量
     * @return 通知列表
     */
    @PostMapping("/notification")
    public R getHomeNotification(Integer num) {
        if (num == null || num < 1) {
            num = 1;
        }
        List<NotificationDO> homeNotification = miniAppApiService.getHomeNotification(num);
        if (homeNotification != null && homeNotification.size() > 0) {
            R ok = R.ok();
            ok.put("notifications", homeNotification);
            return ok;
        }
        return R.error();
    }

    /**
     * 获取首页的课程推荐
     *
     * @param type 推荐类型 0精选热门课程；1名师进修课程；2热门推荐课程
     * @return 课程列表
     */
    @PostMapping("/recommendCourse")
    public R getRecommendCourse(Integer type) {
        if (type == null || type < 0) {
            type = 0;
        }
        List<RecommendCourseVO> recommendCourse = miniAppApiService.getRecommendCourse(type);
        if (recommendCourse != null && recommendCourse.size() > 0) {
            R ok = R.ok();
            ok.put("courses", recommendCourse);
            return ok;
        }
        return R.error();
    }

    /**
     * 获取门店列表
     *
     * @return 门店列表
     */
    @PostMapping("/shops")
    public R getShops() {
        List<ShopsDO> shops = miniAppApiService.getShops();
        if (shops != null && shops.size() > 0) {
            R ok = R.ok();
            ok.put("shops", shops);
            return ok;
        }
        return R.error();
    }

    // ===============自定义页面相关===============

    /**
     * 获取自定义页面
     *
     * @param id 页面ID
     * @return 自定义页面
     */
    @PostMapping("/customPage/{id}")
    public R getCustomPageById(@PathVariable Integer id) {
        if (id == null || id < 1) {
            return R.error();
        }
        CustomPageDO customPageDO = miniAppApiService.getCustomPageById(id);
        if (customPageDO != null) {
            R ok = R.ok();
            ok.put("page", customPageDO);
            return ok;
        }
        return R.error();
    }


    // ===============商品相关===============

    /**
     * 通过ID查询商品详情
     *
     * @param id
     * @return
     */
    /*@PostMapping("/productDetail/{id}")
    public R getProductDetail(@PathVariable Integer id) {
        if (id == null || id < 0) {
            return R.error("你查询的商品不存在");
        }
        // TODO: 20/04/08 商品详情
        List<GoodsVO> productDetail = miniAppApiService.getProductDetail(id);
        if (productDetail == null) {
            return R.error("你查询的商品不存在");
        }
        R ok = R.ok();
        ok.put("product", productDetail.get(0));
        return ok;
    }*/

    // ===============约课相关===============

    /**
     * 获取场馆列表
     *
     * @return
     */
    @PostMapping("/getVenues")
    public R getMyVenueUsers() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", 999);
        Map<String, Object> result = new HashMap<>();
        List<VenueInfoVO> venueInfoVOS = venueInfoService.selectVenueInfoList(map);
        result.put("venues", venueInfoVOS);
        return R.ok(result);
    }

    /**
     * 通过时间查询团体课程
     *
     * @param date
     * @return
     */
    @PostMapping("/getAppointmentCourse")
    public R getAppointmentCourseListByDate(String memberId, String date, String venueId) {
        if (date == null) {
            return R.error();
        }
        /*List<AppointmentClassVO> appointmentCourseList = miniAppApiService.getAppointmentCourseListByDate(date);
        if (appointmentCourseList == null || appointmentCourseList.size() == 0) {
            return R.error();
        }*/
        List<ClassInfoVO> normalClass = new ArrayList<>();
        List<ClassInfoVO> privateClass = new ArrayList<>();
        List<Map<String, Object>> coaches = new ArrayList<>();
        List<ClassInfoVO> classList = miniAppApiService.getClassList(memberId, date);
        classList.forEach(item -> {
            if (!StringUtils.isEmpty(venueId) && item.getVenueId().equals(venueId) || StringUtils.isEmpty(venueId)) {
                // 私教
                if (item.getType().equals("3")) {
                    privateClass.add(item);
                    Map<String, Object> map = new HashMap<>();
                    map.put("coachId", item.getCoachId());
                    map.put("coachName", item.getCoachName());
                    if (!coaches.contains(map)) {
                        coaches.add(map);
                    }
                }
                // 团课
                else {
                    normalClass.add(item);
                }
            }
        });
        R ok = R.ok();
        ok.put("coaches", coaches);
        ok.put("courses", normalClass);
        ok.put("privateCourses", privateClass);
        return ok;
    }

    /**
     * 通过课程ID获取课程详情
     *
     * @param id 课程ID
     * @return
     */
    /*@PostMapping("/getCourseDetail/{id}")
    public R getCourseDetailById(@PathVariable Integer id) {
        if (id == null || id < 0) {
            return R.error();
        }
        CourseVO courseDetailById = miniAppApiService.getCourseDetailById(id);
        R ok = R.ok();
        ok.put("course", courseDetailById);
        return ok;
    }*/

    /**
     * 提交预约
     *
     * @param curriculumId 课程ID
     * @param memberId     会员ID
     * @return
     */
    @PostMapping("/submitAppointment")
    public R submitAppointment(String curriculumId, String memberId, String remark) {
        if (curriculumId == null || memberId == null) {
            return R.error();
        }
        // 是否有权限预约
        boolean hasRights = bookingCourseMapper.hasRightsToAppointment(curriculumId, memberId) > 0;
        if (!hasRights) {
            return R.ok("暂无预约资格");
        }
        Integer alreadyAppointment = bookingCourseMapper.isAlreadyAppointment(curriculumId, memberId);
        if (alreadyAppointment == 0) {
            BookingCourseDTO bookingCourseDTO = new BookingCourseDTO();
            bookingCourseDTO.setId(UUID.randomUUID().toString());
            bookingCourseDTO.setClassId(curriculumId);
            bookingCourseDTO.setMemberId(memberId);
            bookingCourseDTO.setZb(remark);
            bookingCourseDTO.setBookingNum(1);
            bookingCourseDTO.setStatus("1");
            int insert = bookingCourseMapper.insert(bookingCourseDTO);
            if (insert > 0) {
                return R.ok("预约成功！");
            } else {
                return R.error();
            }
        } else {
            return R.ok("您已经预约过了");
        }

        /*if (!miniAppApiService.isAlreadyAppointment(curriculumId, memberId)) {
            boolean b = miniAppApiService.submitAppointment(curriculumId, memberId);
            if (b) {
                return R.ok("预约成功！");
            } else {
                return R.error();
            }
        } else {
            return R.ok("您已经预约过了");
        }*/
    }

    /**
     * 查询已约课程
     *
     * @param memberId 会员ID
     * @param date     日期
     * @return
     */
    @PostMapping("/alreadyAppointment")
    public R getAlreadyAppoint(String memberId, String date) {
        R ok = R.ok();
        if (StringUtils.isBlank(memberId)) {
            return R.error("需要memberId");
        }
        if (StringUtils.isBlank(date)) {
            return R.error("需要date");
        }
        List<ClassInfoVO> classVOList = miniAppApiService.getAlreadyAppointmentCourseListByMemberId(memberId, date);
        ok.put("courses", classVOList);
        return ok;
    }

    /**
     * 查询已约课程的所有历史
     *
     * @param memberId 会员ID
     * @return
     */
    @PostMapping("/appointmentHistory")
    public R getAlreadyAppoint(String memberId) {
        R ok = R.ok();
        if (StringUtils.isBlank(memberId)) {
            return R.error("需要memberId");
        }
        List<ClassInfoVO> classVOList = miniAppApiService.getAppointmentCourseHistoryByMemberId(memberId);
        ok.put("courses", classVOList);
        return ok;
    }

    /**
     * 取消预约
     *
     * @param appointmentId 已预约的ID
     * @return
     */
    @PostMapping("/cancelAppointment")
    public R cancelAppointment(String appointmentId) {
        R ok = R.ok("取消成功");
        if (appointmentId == null || "".equalsIgnoreCase(appointmentId)) {
            return R.error("需要id");
        }
        boolean b = miniAppApiService.cancelAppointment(appointmentId);
        if (b) {
            return ok;
        } else {
            return R.error("请稍后再试");
        }
    }

    /**
     * 获取场馆列表
     *
     * @return 场馆列表
     */
    @PostMapping("/getVenuesList")
    public R listVenues() {
        R ok = R.ok();
        List<VenueInfoVO> venueInfoVOS = venueInfoService.selectVenueInfoList(new HashMap<>());
        ok.put("list", venueInfoVOS);
        return ok;
    }

    /**
     * 获取积分列表
     *
     * @param memberId 会员ID
     * @return 积分列表
     */
    @RequestMapping("/getIntegrals/{id}")
    public R getIntegrals(@PathVariable("id") String memberId) {
        List<MemberIntegralRecordDTO> memberIntegral = memberIntegralRecordMapper.getMemberIntegral(memberId);
        R ok = R.ok();
        ok.put("recode", memberIntegral);
        return ok;
    }

    /**
     * 获取会员体测信息
     *
     * @param memberId 会员ID
     * @return 体测信息列表
     */
    @RequestMapping("/getBodyInfo/{id}")
    public R getMemberBodyInfo(@PathVariable("id") String memberId) {
        List<MemberBodyInfoDTO> memberBodyInfo = memberBodyInfoMapper.getMemberBodyInfo(memberId);
        R ok = R.ok();
        ok.put("recode", memberBodyInfo);
        return ok;
    }

    /**
     * 获取会员会员卡信息
     *
     * @param memberId 会员ID
     * @return 会员卡列表
     */
    @RequestMapping("/getVipInfo/{id}")
    public R getMemberVipInfo(@PathVariable("id") String memberId) {
        List<MemberCardBasicInfoVO> vipCards = miniAppApiService.getVipCards(memberId);
        R ok = R.ok();
        ok.put("recode", vipCards);
        return ok;
    }


}
